var path = "";
$(document).ready(
    function () {
        // 获取项目根路径
        //path = $("#path").val();

        //主菜单第一次添加菜单
        $(document).on("click", ".js_addL1Btn", function () {
            $("#editForm")[0].reset();
            resetSelect();
            var menuSize = $(".pre_menu_item").length;
            $(this).find(".js_addMenuTips").remove();
            $(".pre_menu_list").removeClass("no_menu");
            $("#jsDelBt").removeAttr("subMenu-id");
            $(".sub_pre_menu_box").hide();
            $("#hasSubMenu").show();
            $("#hasSubMenu").attr("checked", "checked");
            $(".otherSet").show();
            $("#js_none").hide();
            $("#js_rightBox").show();
            var uuid = getuuid();
            var html = '';
            switch (menuSize) {
                case 1:
                    $(this).parent().removeClass("size1of1");
                    $(this).parent().addClass("size1of2");
                    $(this).parent().removeClass("size1of3");
                    html += '<li class="jsMenu pre_menu_item grid_item  size1of2 current" status="newMenu" id="menu_' + uuid + '">';
                    html += '<a href="javascript:void(0);" class="pre_menu_link mainMenu"> <i class="icon_menu_dot js_icon_menu_dot dn"></i> <i class="icon20_common sort_gray"></i> <span class="js_lTitle">菜单名称</span>';
                    html += '</a>';
                    html += '<div class="sub_pre_menu_box js_l2TitleBox" style="display:none">';
                    html += '<ul class="sub_pre_menu_list">';
                    html += '<li class="js_addMenuBox"><a href="javascript:void(0);" class="jsSubView"  title="最多添加5个子菜单"><span class=""><i class="icon14_menu_add"></i></span></a></li>';
                    html += '</ul><i class="arrow arrow_out"></i> <i class="arrow arrow_in"></i>';
                    html += '</div></li>';
                    $(this).parent().before(html);

                    $("#menuName").val($("#menu_" + uuid).find(".js_lTitle").text());
                    $(".global_info").text($("#menu_" + uuid).find(".js_lTitle").text());
                    $("#jsDelBt").attr("menu-id", "menu_" + uuid);
                    $("#menu-id").val("menu_" + uuid);
                    break;
                case 2:
                    $(this).parent().removeClass("size1of1");
                    $(this).parent().addClass("size1of3");
                    $(this).parent().removeClass("size1of2");
                    $(".jsMenu").removeClass("current");
                    $(".jsMenu").removeClass("size1of1");
                    $(".jsMenu").addClass("size1of3");
                    $(".jsMenu").removeClass("size1of2");
                    html += '<li class="jsMenu pre_menu_item grid_item  size1of3 current" status="newMenu" id="menu_' + uuid + '">';
                    html += '<a href="javascript:void(0);" class="pre_menu_link mainMenu"> <i class="icon_menu_dot js_icon_menu_dot dn"></i> <i class="icon20_common sort_gray"></i> <span class="js_lTitle">菜单名称</span>';
                    html += '</a>';
                    html += '<div class="sub_pre_menu_box js_l2TitleBox" style="display:none">';
                    html += '<ul class="sub_pre_menu_list">';
                    html += '<li class="js_addMenuBox"><a href="javascript:void(0);" class="jsSubView" title="最多添加5个子菜单"><span class=""><i class="icon14_menu_add"></i></span></a></li>';
                    html += '</ul><i class="arrow arrow_out"></i> <i class="arrow arrow_in"></i>';
                    html += '</div></li>';
                    $(this).parent().before(html);
                    $("#menuName").val($("#menu_" + uuid).find(".js_lTitle").text());
                    $(".global_info").text($("#menu_" + uuid).find(".js_lTitle").text());
                    $("#jsDelBt").attr("menu-id", "menu_" + uuid);
                    $("#menu-id").val("menu_" + uuid);
                    break;
                case 3:
                    $(".jsMenu").removeClass("current");
                    html += '<li class="jsMenu pre_menu_item grid_item  size1of3 current" status="newMenu" id="menu_' + uuid + '">';
                    html += '<a href="javascript:void(0);" class="pre_menu_link mainMenu"> <i class="icon_menu_dot js_icon_menu_dot dn"></i> <i class="icon20_common sort_gray"></i> <span class="js_lTitle">菜单名称</span>';
                    html += '</a>';
                    html += '<div class="sub_pre_menu_box js_l2TitleBox" style="display:none">';
                    html += '<ul class="sub_pre_menu_list">';
                    html += '<li class="js_addMenuBox"><a href="javascript:void(0);" class="jsSubView" title="最多添加5个子菜单"><span class=""><i class="icon14_menu_add"></i></span></a></li>';
                    html += '</ul><i class="arrow arrow_out"></i> <i class="arrow arrow_in"></i>';
                    html += '</div></li>';
                    $(this).parent().hide();
                    $(this).parent().before(html);
                    $("#menuName").val($("#menu_" + uuid).find(".js_lTitle").text());
                    $(".global_info").text($("#menu_" + uuid).find(".js_lTitle").text());
                    $("#jsDelBt").attr("menu-id", "menu_" + uuid);
                    $("#menu-id").val("menu_" + uuid);
                    break;

            }


        });

        //主菜单选择
        $(document).on("click", ".pre_menu_item .mainMenu", function () {
            mainMenuSelect(this);
        });
        //添加子菜单
        $(document).on("click", ".jsSubView", function () {
            $("#editForm")[0].reset();
            resetSelect();
            $(".otherSet").show();
            $(".js_setKeyBox").show();
            $(".js_setUrlBox").show();
            $("#js_rightBox").show();
            $("#js_none").hide();
            var subMenuSize = $(this).parent().parent().find(".subMenu").length;
            if (subMenuSize == 5) {
                alert("最多只能添加5个菜单");
                return;
            }
            $(".hasSubMenu").hide();
            $(".current").removeClass("current");
            var uuid = getuuid();
            var html = '<li id="' + uuid + '_subMenu" status="newMenu" class="subMenu current"><a href="javascript:void(0);"  class="subMenuEdit"><span>';
            html += '<i class="icon20_common sort_gray"></i>';
            html += '<span class="subMenuTitle">子菜单名称</span></span></a></li>';
            $(this).parent().parent().prepend(html);

            $("#menuName").val($("#" + uuid + "_subMenu .subMenuTitle").text());
            $(".global_info").text($("#" + uuid + "_subMenu .subMenuTitle").text());
            $("#jsDelBt").attr("subMenu-id", uuid + "_subMenu");
            $("#jsDelBt").attr("menu-id", $(this).parent().parent().parent().parent().attr("id"));
            $("#menu-id").val(uuid + "_subMenu");
        });

        //子菜单选择
        $(document).on("click", ".subMenuEdit", function () {
            //查询数据
            $("#editForm")[0].reset();
            resetSelect();
            var status = $(this).parent().attr("status");
            if (status === "oldMenu") {
                $.post("wechatMenu/query-submenu-by-code", {menuCode: $(this).parent().attr("id")}, function (result) {
                    var data = result.data;
                    $("#menuType option").map(function () {
                        if (data.type === $(this).val()) {
                            $(this).attr("selected", "selected");
                        }
                    });

                    if ($("#menuType").val() === "view") {
                        $(".js_setKeyBox").hide();
                        $(".js_setUrlBox").show();
                        $("#menuUrl").val(data.url);
                    } else {
                        $("#menuKey").val(data.sKey);
                        $(".js_setKeyBox").show();
                        $(".js_setUrlBox").hide();
                    }
                });
            }
            $(".otherSet").show();

            //添加数据到编辑面板
            $(".hasSubMenu").hide();
            $("#menuName").val($(this).find(".subMenuTitle").text());
            $(".global_info").text($(this).find(".subMenuTitle").text());
            $("#js_none").hide();
            $("#js_rightBox").show();
            $(".current").removeClass("current");
            $(".subMenu").removeClass("current");
            $(this).parent().addClass("current");
            $("#jsDelBt").attr("subMenu-id", $(this).parent().attr("id"));
            $("#jsDelBt").attr("menu-id", $(this).parent().parent().parent().parent().attr("id"));


        });
        //不设置子菜单按钮点击事件
        $("#hasSubMenu").click(function () {

            if ($(this).is(":checked")) {
                var subMenuSize = $("#" + $("#jsDelBt").attr("menu-id")).find(".subMenu").length;
                //当子菜单个数大于0表示含有子菜单提示是否删除子菜单
                if (subMenuSize > 0) {
                    //如果数据确认删除子菜单
                    if (confirm("编辑主菜单你是否要删除子菜单?")) {
                        //删除数据库中的子菜单
                        $.post("wechatMenu/delete-all-sub-menu", {code: $("#jsDelBt").attr("menu-id")}, function (result) {
                            var data = result.data;
                            if (data == "success") {
                                //移出相应的子菜单
                                $.post(path + "/wechatMenu/update-menu-hasSubMenu", {
                                    hasSubMenu: 'yes',
                                    menuCode: menuId
                                }, null);
                                $("#" + $("#jsDelBt").attr("menu-id")).find(".subMenu").remove();
                            }
                        });

                    } else {
                        $(this).attr("checked", false);
                    }

                }
                $("#" + $("#jsDelBt").attr("menu-id")).find(".sub_pre_menu_box").hide();
                $(".js_setKeyBox").show();
                $(".js_setUrlBox").show();
                $(".otherSet").show();

            } else {
                $("#" + $("#jsDelBt").attr("menu-id")).find(".sub_pre_menu_box").show();
                $(".js_setKeyBox").show();
                $(".js_setUrlBox").show();
                $(".otherSet").hide();
            }
        });
        //菜单类型选择
        $("#menuType").change(function () {
            if ($(this).val() == "view") {
                $(".js_setKeyBox").hide();
                $(".js_setUrlBox").show();
            } else if ($(this).val() == "click") {
                $(".js_setKeyBox").show();
                $(".js_setUrlBox").hide();
            } else {
                $(".js_setKeyBox").show();
                $(".js_setUrlBox").show();
            }
        });
        //删除菜单
        $("#jsDelBt").click(function () {
            var subMenuId = $(this).attr("submenu-id");
            var menuId = $(this).attr("menu-id");
            //当子菜单存在时删除子菜单不存在时删除主菜单
            if (subMenuId != undefined) {
                if (confirm("你确定要删除该菜单吗？")) {
                    $("#" + subMenuId).remove();
                    //删除数据库中的菜单
                    $.post("wechatMenu/delete-sub-menu", {code: subMenuId}, function (status) {
                        if (status.code == "1000") {
                            var subMenuSize = $("#" + menuId).find(".subMenu").length;
                            if (subMenuSize == 0) {
                                $("#hasSubMenu").attr("checked", "checked");
                                $(".otherSet").show();
                                $("#js_none").hide();
                                $("#js_rightBox").show();
                                $("#" + menuId).addClass("current");
                                //主菜单选中
                                //更改主菜单状态
                                $.post("wechatMenu/update-menu-hasSubMenu", {
                                    hasSubMenu: 'yes',
                                    menuCode: menuId
                                }, function () {
                                    noMenuSelect($("#" + menuId).find(".mainMenu"));
                                    alert("请重新设置主菜单！！");
                                });

                            } else {
                                $("#js_none").show();
                                $("#js_rightBox").hide();
                            }
                        }
                    });

                }
            } else {

                if (confirm("你确定要删除该菜单吗？\n下面子菜单将全部删除")) {
                    //获取主菜单个数
                    var menuSize = $(".jsMenu").length;
                    $(".pre_menu_list").removeClass("size1of" + menuSize);

                    //删除数据库下的主菜单和所有子菜单
                    $.post("wechatMenu/delete-menu", {code: menuId}, function (status) {
                        //移出当前选择的主菜单

                        $("#" + menuId).remove();
                        //设置菜单宽度
                        if (menuSize > 0) {
                            $(".pre_menu_item").addClass("size1of" + menuSize);
                        }
                        //显示添加菜单按钮
                        $(".js_addMenuBox").show();
                        if (menuSize == 1) {

                            //没有菜单时初始到开始状态。
                            $(".pre_menu_list").addClass("no_menu");
                            $(".js_addL1Btn").append('<span class="js_addMenuTips">添加菜单</span>');
                        }
                        //隐藏编辑面板
                        $("#js_none").show();
                        $("#js_rightBox").hide();
                    });
                }
            }

        });
        //添加菜单
        $(".addMenuBtn").click(function () {
            var wechatId = $("#wechatId").val();
            var menuId = $("#jsDelBt").attr("menu-id");
            var subMenuId = $("#jsDelBt").attr("submenu-id");
            var menuName = $("#menuName").val();
            var menuType = $("#menuType").val();
            var menuUrl = $("#menuUrl").val();
            var menuKey = $("#menuKey").val();
            var hasSubMenu = $("input[id='hasSubMenu']:checked").val();

            if (menuName == "") {
                alert("菜单名不能为空！！");
                return;
            }

            if (subMenuId != undefined) {
                if (menuType == "") {
                    alert("菜单类型不能为空！");
                    return;
                }
                if (menuType == "view") {
                    if (menuUrl === "") {
                        alert("菜单URL不能为空！");
                        return;
                    }
                }
                if (menuType == "click") {
                    if (menuKey === "") {
                        alert("菜单Key不能为空！");
                        return;
                    }
                }
                if ($("#" + subMenuId).attr("status") == "newMenu") {
                    //新菜单添加
                    $.post("wechatMenu/add-sub-menu", {
                        menuId: menuId, subMenuId: subMenuId,
                        menuName: menuName, menuType: menuType,
                        menuUrl: menuUrl, menuKey: menuKey
                    }, function (result) {

                        if (result.code === "1000") {
                            $("#" + subMenuId).attr("status", "oldMenu");
                            $("#" + subMenuId).find(".subMenuTitle").text(menuName);
                        } else {
                            alert("系统出错，添加出错");
                            $(".global_info").text(menuName);
                        }
                    });
                } else {
                    $.post("wechatMenu/update-submenu", {
                        menuCode: subMenuId,
                        menuName: menuName, menuType: menuType,
                        menuUrl: menuUrl, menuKey: menuKey
                    }, function (result) {
                        if (result.code === "1000") {
                            $("#" + subMenuId).attr("status", "oldMenu");
                            $("#" + subMenuId).find(".subMenuTitle").text(menuName);
                        } else {
                            alert("系统出错，修改出错");
                            $(".global_info").text(menuName);
                        }
                    });
                }
            } else {

                if (hasSubMenu != undefined && hasSubMenu === "1") {
                    hasSubMenu = "yes";
                    if (menuType == "") {
                        alert("菜单类型不能为空！");
                        return;
                    }

                    if (menuType == "view") {
                        if (menuUrl === "") {
                            alert("菜单URL不能为空！");
                            return;
                        }
                    }
                    if (menuType == "click") {
                        if (menuKey === "") {
                            alert("菜单Key不能为空！");
                            return;
                        }
                    }
                } else if (hasSubMenu == undefined) {
                    hasSubMenu = "no";

                }
                if ($("#" + menuId).attr("status") == "newMenu") {
                    $.post("wechatMenu/add-menu", {
                        wechatId: wechatId, menuId: menuId, subMenuId: subMenuId,
                        menuName: menuName, menuType: menuType,
                        menuUrl: menuUrl, menuKey: menuKey, hasSubMenu: hasSubMenu
                    }, function (status) {

                        if (status.code == "1000") {
                            $("#" + menuId).attr("status", "oldMenu");
                            $("#" + menuId).find(".js_lTitle").text(menuName);
                            $(".global_info").text(menuName);
                        } else {
                            alert("系统出错，添加出错");
                        }
                    });
                } else {
                    $.post("wechatMenu/update-menu", {
                        menuCode: menuId,
                        menuName: menuName, menuType: menuType,
                        menuUrl: menuUrl, menuKey: menuKey, hasSubMenu: hasSubMenu
                    }, function (status) {
                        if (status.code == "1000") {
                            $("#" + menuId).attr("status", "oldMenu");
                            $("#" + menuId).find(".js_lTitle").text(menuName);
                            $(".global_info").text(menuName);
                        } else {
                            alert("系统出错，修改出错");
                        }
                    });
                }
            }
        });
        //提交菜单
        $(".submitBtn").click(function () {
            if (confirm("确定要创建该公众号菜单了吗?")) {
                $("#submiting").show();
                $.post("wechatMenu/submit-menu", {wechatId: $("#wechatId").val()}, function (result) {
                    $("#submiting").hide();
                    if (result.data == "error") {
                        alert("系统出错，创建失败！！");
                    } else if (result.data.errmsg === "ok") {
                        alert("菜单创建成功！24小时内生效！请打开微信进行查看。");
                    } else {
                        alert("菜单创建失败！！错误信息:{" + result.data.errcode + "},{" + result.data.errmsg + "}");
                    }

                }, 'json');
            }
        });
    });

/**
 * 初始化菜单
 */
function mainMenuSelect(obj) {
    $("#editForm")[0].reset();
    resetSelect();
    //查询数据
    var status = $(obj).parent().attr("status");

    if (status === "oldMenu") {
        $.post("wechatMenu/query-menu-by-code", {menuCode: $(obj).parent().attr("id")}, function (result) {
            var data = result.data;
            var hasSubMenu = data.hasSubMenu;
            //是否设置子菜单处理
            if (hasSubMenu != "" && hasSubMenu == "yes") {
                $("#hasSubMenu").attr("checked", "checked");
                $(".otherSet").show();

                //菜单类型
                $("#menuType option").map(function () {
                    if (data.type === $(this).val()) {
                        $(this).attr("selected", "selected");
                    }
                });
                if ($("#menuType").val() === "view") {
                    $(".js_setKeyBox").hide();
                    $(".js_setUrlBox").show();
                    $("#menuUrl").val(data.url);
                } else {
                    $("#menuKey").val(data.mKey);
                    $(".js_setKeyBox").show();
                    $(".js_setUrlBox").hide();
                }
            } else {
                $("#hasSubMenu").attr("checked", false);
                $(".otherSet").hide();
            }

        });
    }
    //添加数据到编辑面板
    $("#menuName").val($(obj).find("span").text());
    $(".global_info").text($(obj).find("span").text());

    $(".hasSubMenu").show();
    $(".pre_menu_item").removeClass("current");
    //隐藏子其它菜单
    $(".sub_pre_menu_box").hide();

    //当前只菜单显示
    $(obj).parent().find(".sub_pre_menu_box").show();
    $(obj).parent().find(".sub_pre_menu_box ul li").removeClass("current");

    //当前菜单选中
    $(obj).parent().addClass("current");
    $(obj).addClass("pre_menu_link");
    $("#jsDelBt").attr("menu-id", $(obj).parent().attr("id"));
    $("#jsDelBt").removeAttr("subMenu-id");
    $("#js_rightBox").show();
    $("#js_none").hide();
}

function noMenuSelect(obj) {
    $("#editForm")[0].reset();
    resetSelect();
    //查询数据
    var status = $(obj).parent().attr("status");

    if (status === "oldMenu") {
        $.post("wechatMenu/query-menu-by-code", {menuCode: $(obj).parent().attr("id")}, function (result) {
            var data = result.data;
            var hasSubMenu = data.hasSubMenu;
            //是否设置子菜单处理

            $("#hasSubMenu").attr("checked", "checked");
            $(".otherSet").show();

            //菜单类型
            $("#menuType option").map(function () {
                if (data.type === $(this).val()) {
                    $(this).attr("selected", "selected");
                }
            });
            if ($("#menuType").val() === "view") {
                $(".js_setKeyBox").hide();
                $(".js_setUrlBox").show();
                $("#menuUrl").val(data.url);
            } else {
                $("#menuKey").val(data.mKey);
                $(".js_setKeyBox").show();
                $(".js_setUrlBox").hide();
            }

        });
    }
    //添加数据到编辑面板
    $("#menuName").val($(obj).find("span").text());
    $(".global_info").text($(obj).find("span").text());

    $(".hasSubMenu").show();
    $(".pre_menu_item").removeClass("current");
    //隐藏子其它菜单
    $(".sub_pre_menu_box").hide();

    //当前只菜单显示
    $(obj).parent().find(".sub_pre_menu_box").show();
    $(obj).parent().find(".sub_pre_menu_box ul li").removeClass("current");

    //当前菜单选中
    $(obj).parent().addClass("current");
    $(obj).addClass("pre_menu_link");
    $("#jsDelBt").attr("menu-id", $(obj).parent().attr("id"));
    $("#jsDelBt").removeAttr("subMenu-id");
    $("#js_rightBox").show();
    $("#js_none").hide();
}

function initMenus(id) {
    $("#editForm")[0].reset();
    resetSelect();
    $.post("wechatMenu/menu-list", {wechatId: id}, function (result) {

        var html = "";
        var size = 1;
        var data = result.data;
        if (data.length >= 3) {
            size = 3;
            $(".js_addMenuBox").hide();
        } else {
            $(".js_addMenuBox").show();
            size = data.length + 1;
        }
        if (data.length > 0) {
            $(".pre_menu_list").removeClass("no_menu");
            $(".js_addMenuBox").find(".js_addMenuTips").remove();
            $("#jsDelBt").removeAttr("subMenu-id");
            $(".sub_pre_menu_box").hide();
            $("#js_none").show();
            $("#js_rightBox").hide();
        } else {
            if ($(".js_addMenuTips").text() == "") {
                $(".js_addL1Btn").append('<span class="js_addMenuTips">添加菜单</span>');
            }
            $(".pre_menu_list").addClass("no_menu");
            $("#js_none").show();
            $("#js_rightBox").hide();
        }
        $("#menuList").find(".jsMenu").remove();
        for (var i = 0; i < data.length; i++) {
            var subMenus = data[i].subMenus;
            html += '<li class="jsMenu pre_menu_item grid_item  size1of' + size + '" status="oldMenu" id="' + data[i].code + '">';
            html += '<a href="javascript:void(0);" class="pre_menu_link mainMenu"> <i class="icon_menu_dot js_icon_menu_dot dn"></i> <i class="icon20_common sort_gray"></i> <span class="js_lTitle">' + data[i].name + '</span>';
            html += '</a>';
            html += '<div class="sub_pre_menu_box js_l2TitleBox" style="display:none;">';
            html += '<ul class="sub_pre_menu_list">';
            for (var j = 0; j < subMenus.length; j++) {
                html += '<li id="' + subMenus[j].subCode + '" class="subMenu" status="oldMenu"><a href="javascript:void(0);"  class="subMenuEdit"><span>';
                html += '<i class="icon20_common sort_gray"></i>';
                html += '<span class="subMenuTitle">' + subMenus[j].name + '</span></span></a></li>';
            }

            html += '<li class="js_addMenuBox"><a href="javascript:void(0);" class="jsSubView" title="最多添加5个子菜单"><span class=""><i class="icon14_menu_add"></i></span></a></li>';
            html += '</ul><i class="arrow arrow_out"></i> <i class="arrow arrow_in"></i>';
            html += '</div></li>';
            $(".js_addL1Btn").parent().before(html);

            html = "";
        }
        $(".js_addL1Btn").parent().removeClass("size1of1");
        $(".js_addL1Btn").parent().addClass("size1of" + size);
        dialogPos("#js_edit_menu");
    });


}

/**
 * 编辑菜单
 * @param id
 */
function editMenu(id, wechatName) {

    $(".tc").text(wechatName);
    $("#wechatId").val(id);
    //查询该公众号下的菜单
    initMenus(id);
}

function resetSelect() {
    $("#menuType option").attr("selected", false);

}

function getuuid() {
    var s = [];
    var hexDigits = "0123456789abcdef";
    for (var i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    }
    s[14] = "4";  // bits 12-15 of the time_hi_and_version field to 0010
    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);  // bits 6-7 of the clock_seq_hi_and_reserved to 01
    s[8] = s[13] = s[18] = s[23] = "-";

    var uuid = s.join("");
    return uuid;
}

function dialogPos(id) {
    var self = this;
    $("body").css("overflow", "hidden");
    $("html").css("overflow", "visible");
    $(id).css("display", "block");
    var $dialog_main = $(id).find(".dialog_main")
    var num = getRandomNum(-100, 100);
    if ($(id).siblings(".dialog").is(":visible")) {
        if ($dialog_main.outerHeight() > $(window).height()) {
            $dialog_main.css({
                top: 0,
                left: ($(window).width() - $dialog_main.outerWidth()) / 2
                    + num + "px"
            });
        } else {
            $dialog_main.css({
                top: ($(window).height() - $dialog_main.outerHeight()) / 2
                    + "px",
                left: ($(window).width() - $dialog_main.outerWidth()) / 2
                    + num + "px"
            });
        }
    } else {
        if ($dialog_main.outerHeight() > $(window).height()) {
            $dialog_main.css({
                top: 0,
                left: ($(window).width() - $dialog_main.outerWidth()) / 2
                    + "px"
            });
        } else {
            $dialog_main.css({
                top: ($(window).height() - $dialog_main.outerHeight()) / 2
                    + "px",
                left: ($(window).width() - $dialog_main.outerWidth()) / 2
                    + "px"
            });
        }
    }
    $(window).resize(
        function () {
            if ($dialog_main.outerHeight() > $(window).height()) {
                $dialog_main.css({
                    top: 0,
                    left: ($(window).width() - $dialog_main.outerWidth())
                        / 2 + "px"
                });
            } else {
                $dialog_main.css({
                    top: ($(window).height() - $dialog_main.outerHeight())
                        / 2 + "px",
                    left: ($(window).width() - $dialog_main.outerWidth())
                        / 2 + "px"
                });
            }
        });
    return self;

}

function getRandomNum(Min, Max) {
    var Range = Max - Min;
    var Rand = Math.random();
    return (Min + Math.round(Rand * Range));
}